package com.hongxun.budget.ui.activity.ys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.ui.adapter.SpinnerAdapter;
import com.hongxun.budget.utils.DBUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.T;
import com.hongxun.budget.utils.TestingUtils;
import com.hongxun.greenentity.tk.BDataDictionaryEntity;
import com.hongxun.greenentity.ys.EDeviceOldEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 编辑点位原始设备信息
 */
public class YsEditePointDeviceActivity extends BaseActivity {

    @BindView(R.id.right_save)
    Button rightSave;
    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.code)
    EditText code;
    @BindView(R.id.parameter)
    EditText parameter;

    SpinnerAdapter<BDataDictionaryEntity> spinnerAdapter;

    EDeviceOldEntity d;
    int index = 0;

    @Override
    public  int getLayoutId() {
        return R.layout.activity_edite_point_device;
    }

    @Override
    public  void initView(Bundle savedInstanceState) {
        Intent intent = getIntent();
        index = intent.getIntExtra("index", 0);
        if (d.getDRWork().equals("修理")) {
            findViewById(R.id.option_layout).setVisibility(View.GONE);
        }
        d = (EDeviceOldEntity) intent.getSerializableExtra("device");
        rightSave.setVisibility(View.VISIBLE);
        List<BDataDictionaryEntity> entities = DBUtils.getBDataDictionaryEntities("opinion_type");
        spinner.setAdapter(spinnerAdapter = new SpinnerAdapter<BDataDictionaryEntity>(context, entities, spinner));
        code.setText(TextUtils.isEmpty(d.getDRCode()) ? "" : d.getDRCode());
        name.setText(d.getDRName());
        parameter.setText(d.getDRParameter());
        int i = 0;
        for (BDataDictionaryEntity b : entities) {
            if (b.getDKey().equals(d.getDROpinion())) {
                spinner.setSelection(i);
                break;
            }
            i++;
        }
    }

    @OnClick(R.id.right_save)
    public void onViewClicked() {
        String c = getText(code);
        String n = getText(name);
        String p = getText(parameter);
        if (!TestingUtils.isEmpty(context, n, "名称")) return;
        if (!TestingUtils.isEmpty(context, p, "技术参数")) return;
        d.setDRParameter(p);
        d.setDRName(n);
        d.setDRCode(c);
        d.setDROpinion(spinnerAdapter.getSelectedItem().getDKey());
        d.setDRDatastate("修改");
        //记录处置意见
        SPUtils.put(context, "opinion_type", spinnerAdapter.getSelectedItem().getDKey());
        Intent intent = new Intent();
        intent.putExtra("device", d);
        intent.putExtra("index", index);
        setResult(Activity.RESULT_OK, intent);
        finish();
        T.showLong(context, "保存成功");
    }
}
